package com.farmer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.farmer.pojo.Policy;
import com.farmer.service.PolicyService;
import com.farmer.mapper.PolicyMapper;
import com.farmer.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;

/**
* @author 86183
* @description 针对表【policy(扶贫政策表)】的数据库操作Service实现
* @createDate 2024-04-27 17:27:03
*/
@Service
public class PolicyServiceImpl extends ServiceImpl<PolicyMapper, Policy>
    implements PolicyService{
    @Autowired
    private PolicyMapper policyMapper;

    @Override
    public Result add(Policy policy) {

        policy.setTime(DateFormat.getDateInstance().format(new Date()));
        policyMapper.insert(policy);
        return Result.ok(null);
    }

    @Override
    public Result delete(Integer id) {
        policyMapper.deleteById(id);
        return null;
    }


    @Override
    public Result update(Policy policy) {
        policyMapper.updateById(policy);
        return Result.ok(null);
    }


    @Override
    public Result getById(Integer id) {
        Policy policy = policyMapper.selectById(id);
        if (policy != null){
            return Result.build(policy,111,"查询成功");
        }else{
            return Result.build(null,101,"查询失败");
        }
    }

    @Override
    public Result getAll() {
        List<Policy> policies = policyMapper.selectList(null);
       if (policies != null&&!policies.isEmpty()){
           return Result.build(policies,111,"查询成功");
       }else{
           return Result.build(null,101,"查询失败");
       }
    }

    @Override
    public Result getByTitleLike(String title){
        LambdaQueryWrapper<Policy> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Policy::getName,title);
        List<Policy> policies = policyMapper.selectList(lambdaQueryWrapper);
        if (policies!=null&&!policies.isEmpty()){
            return Result.build(policies,111,"查询成功");
        }else{
            return Result.build(null,101,"未找到匹配信息");
        }
    }

    @Override
    public Result show() {
        QueryWrapper<Policy> queryWrapper = new QueryWrapper<>();
        queryWrapper.last("limit 6");
        List<Policy> info = policyMapper.selectList(queryWrapper);
        Result result = new Result<>();
        result.setData(info);
        return result;
    }

    @Override
    public Result deleteBatch(List<Integer> ids){
        for (Integer id :ids) {
            policyMapper.deleteById(id);
        }
        return Result.ok(null);
    }


    /*
     * 分页查询*/
    @Override
    public Result page(Integer pageNum, Integer pageSize){
        // 创建分页构造对象
        Page<Policy> page = new Page<>(pageNum, pageSize);
        // 执行分页查询
        IPage<Policy> result = policyMapper.selectPage(page, null);
        // 封装到统一响应体Result中
        return Result.ok(result);
    }

}




